package com.prestamomo.vil.ppa.pmvilmedium;

import static android.content.Context.TELEPHONY_SERVICE;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import androidx.core.app.ActivityCompat;

import com.prestamomo.vil.ppa.PmvilApp;

import java.lang.reflect.Method;

public class PmvilImeiMd {

    public static String getDeviceIMEI() {
        String pmvilimei = "";
        try {
            TelephonyManager pmviltm = (TelephonyManager) PmvilApp.getPmvilApss().getSystemService(TELEPHONY_SERVICE);
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                if (ActivityCompat.checkSelfPermission(PmvilApp.getPmvilApss(), Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                    return "";
                }
                pmvilimei = pmviltm.getDeviceId();
            } else if (android.os.Build.VERSION.SDK_INT < 29) {
                Method pmvilmethod = pmviltm.getClass().getMethod("getImei");
                pmvilimei = (String) pmvilmethod.invoke(pmviltm);
            } else {
                pmvilimei = Settings.System.getString(PmvilApp.getPmvilApss().getContentResolver(), Settings.Secure.ANDROID_ID).toUpperCase();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(pmvilimei==null||pmvilimei.equals("unknown")){
            pmvilimei="";
        }
        return pmvilimei;
    }
}
